package com.ruibang.glass.portal.controller;

import com.alibaba.fastjson.JSON;
import com.ruibang.glass.common.domain.DictInfo;
import com.ruibang.glass.portal.domain.Tree;
import com.ruibang.glass.portal.entity.DeptEntity;
import com.ruibang.glass.portal.service.DeptService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;


@RestController
@RequestMapping("/api/dept")
@Slf4j
@Api(tags = "部门管理")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @ApiOperation(value = "获取部门列表树", response = ResultBody.class)
    @PostMapping(value = "/getTree")
    public ResultBody<Tree<DeptEntity>> getTree(@RequestBody DeptEntity dept) {
        return deptService.getTree(dept);
    }

    @ApiOperation(value = "获取设备部下部门树", response = ResultBody.class)
    @GetMapping(value = "/getEquipmentDept")
    public ResultBody<Tree<DeptEntity>> getEquipmentDept() {
        return deptService.getEquipmentDept();
    }

    @ApiOperation(value = "保存部门", response = ResultBody.class)
    @PostMapping(value = "/saveDept")
    public ResultBody<Void> saveDept(@RequestBody @Valid DeptEntity dept) {
        return deptService.saveDept(dept);
    }


    @ApiOperation(value = "更新部门信息", response = ResultBody.class)
    @PostMapping(value = "/updateDept")
    public ResultBody updateDept(@RequestBody DeptEntity dept) {
        deptService.updateDept(dept);
        return ResultBody.success();
    }

    @ApiOperation(value = "通过用户 ID 获取 部门信息 ", response = ResultBody.class)
    @PostMapping(value = "/getDeptInfoByUserID/{id}")
    public ResultBody<DeptEntity> getDeptInfoByUserID(@PathVariable(value = "id") String id) {
        return deptService.getDeptInfoByUserID(id);
    }

    @ApiOperation(value = "通过部门编号删除部门信息 ", response = ResultBody.class)
    @PostMapping(value = "/deleteDeptById/{id}")
    public ResultBody deleteDeptById(@PathVariable(value = "id") String id) {
        return deptService.deleteDeptById(id);
    }

    @ApiOperation(value = "通过部门编号获取部门信息 ", response = ResultBody.class)
    @GetMapping(value = "/getById/{id}")
    public DeptEntity getById(@PathVariable("id") String id) {
        return deptService.getById(id);
    }

    @ApiOperation(value = "通过部门编号获取部门信息 ", response = ResultBody.class)
    @GetMapping(value = "/getByDeptId/{deptId}")
    public ResultBody getByDeptId(@PathVariable("deptId") String deptId) {
        return ResultBody.success(deptService.getById(deptId));
    }

    @ApiOperation(value = "根据部门Id列表查询部门名称", response = ResultBody.class)
    @PostMapping(value = "/getByDeptIds")
    public List<DictInfo> getByDeptIds(@RequestBody List<String> deptIds) {
        return deptService.getByDeptIds(deptIds);
    }

    @ApiOperation(value = "根据部门Id列表查询部门名称", response = ResultBody.class)
    @PostMapping(value = "/getDeptByIds")
    public ResultBody getDeptByIds(@RequestBody List<String> deptIds) {
        return ResultBody.success(deptService.getDeptByIds(deptIds));
    }


    @ApiOperation(value = "根据部门Id列表查询用户Id", response = ResultBody.class)
    @PostMapping(value = "/getUserIdsByDeptIds")
    public ResultBody getUserIdsByDeptIds(@RequestBody List<String> deptIds) {
        return ResultBody.success(deptService.getUserIdsByDeptIds(deptIds));
    }

    @ApiOperation(value = "根据部门查询部门经理Id", response = ResultBody.class)
    @GetMapping(value = "/getManagerByDeptId/{deptId}")
    public ResultBody getManagerByDeptId(@PathVariable("deptId") String deptId) {
        return ResultBody.success(deptService.getManagerByDeptId(deptId));
    }
}
